//go:build !windows && !plan9 && !nacl

package sshex

import (
	"os"

	"golang.org/x/term"
)

const (
	pathTTY string = "/dev/tty"
)

func openTTY() (ttyin, ttyout *os.File, err error) {
	if term.IsTerminal(int(os.Stdin.Fd())) {
		ttyin = os.Stdin
	} else {
		ttyin, err = os.OpenFile(pathTTY, os.O_RDWR, 0)
		if err != nil {
			return nil, nil, err
		}
	}
	if term.IsTerminal(int(os.Stdout.Fd())) {
		ttyout = os.Stdout
	} else {
		ttyout, err = os.OpenFile(pathTTY, os.O_RDWR, 0)
		if err != nil {
			return nil, nil, err
		}
	}
	return
}

func closeTTY(ttyin, ttyout *os.File) {
	if ttyin != os.Stdin {
		ttyin.Close()
	}
	if ttyout != os.Stdout {
		ttyout.Close()
	}
}
